package com.china08.yxyapi.api.manage.stu.myclass.moments;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.moments.YxyMoments;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsCommentRepository;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsPraiseRepository;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.ThumborUtil;

/**
 * 班级圈--家长--查看班级圈
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class MomentsStuViewController extends YxyApiBaseController {

    @Autowired
    private YxyMomentsRepository yxyMomentsRepository;

    @Autowired
    private YxyMomentsPraiseRepository yxyMomentsPraiseRepository;

    @Autowired
    private YxyMomentsCommentRepository yxyMomentsCommentRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/moments/stuView", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        YxyProfile yxyProfile = getProfile(request, response);
        String currentUserId = yxyProfile.getId();
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");

        Page<YxyMoments> page = yxyMomentsRepository.findByClassIdIn(reqModel.classIdLst, pageable);

        List<RespModel> aList = new ArrayList<MomentsStuViewController.RespModel>();

        for (YxyMoments yxyMoments : page) {
            RespModel model = new RespModel();
            String momentsId = yxyMoments.getId();
            model.avatar = yxyMoments.getAvatar();

            model.classId = yxyMoments.getClassId();
            model.classNick = yxyMoments.getClassNick();

            // 评论数量
            YxyMoments moments = yxyMomentsRepository.findById(momentsId);
            if (moments != null) {
                Integer count = yxyMomentsCommentRepository.countByMomentsId(momentsId);
                moments.setCommentCount(count);
                yxyMomentsRepository.save(moments);

            }
            model.commentCount = moments.getCommentCount() != null ? moments.getCommentCount() : 0;
            // model.commentCount = yxyMoments.getCommentCount() != null ?
            // yxyMoments.getCommentCount() : 0;
            model.createdDate = DateToolUtils.format(yxyMoments.getCreatedDate(), "yyyy-MM-dd HH:mm");
            model.id = yxyMoments.getId();
            // 图片处理
            List<String> imgList = yxyMoments.getImgs();
            List<String> imgs_60_60 = new ArrayList<String>();
            for (String string : imgList) {
                if (null != string) {
                    string = ThumborUtil.resize(string, 60, 60);
                    imgs_60_60.add(string);
                }

            }
            model.imgs_60_60 = imgs_60_60;

            // 图片数量
            if (yxyMoments.getImgs() != null) {
                model.imgSize = yxyMoments.getImgs().size();
            }
            model.isShareAll = yxyMoments.getIsShareAll();
            model.praiseCount = yxyMoments.getPraiseCount() != null ? yxyMoments.getPraiseCount() : 0;
            model.styleIntro = yxyMoments.getStyleIntro();
            model.senderNick = yxyMoments.getSenderNick();
            model.senderId = yxyMoments.getSenderId();
            model.senderUsername = yxyMoments.getSenderUsername();
            model.praiseUserNickStr = yxyMoments.getPraiseUserNickStr("、");
            model.praise = yxyMomentsPraiseRepository.countByMomentsIdAndUserId(yxyMoments.getId(), currentUserId) > 0;
            aList.add(model);
        }

        Page<RespModel> respModelpage = new PageImpl<MomentsStuViewController.RespModel>(aList, pageable,
                page.getTotalElements());
        return respModelpage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public List<String> classIdLst;

        @Override
        public void autoCheck() {
            Assert.isTrue(classIdLst.size() > 0, "班级ID不能为空");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
        // ID
        public String id;
        // 发布时间
        public String createdDate;
        // 发送者昵称
        public String senderNick;
        // 发送人头像
        public String avatar;
        // 发送者ID
        public String senderId;
        // 发送者用户名
        public String senderUsername;
        // 班级ID
        public String classId;
        // 班级名称
        public String classNick;
        // 班级圈内容
        public String styleIntro;
        // 是否公开
        public Integer isShareAll;
        // 评论数量
        public Integer commentCount;
        // 发布图片
        public List<String> imgs_60_60;
        // 图片数量
        public Integer imgSize = 0;
        // 点赞数量
        public Integer praiseCount;
        // 点赞人信息
        public String praiseUserNickStr;
        // 是否点赞
        public boolean praise;
        // 是否自己的班级圈
        public boolean ownMoments;

    }
}
